package com.life.goal.adapter.web.controller;

import com.life.goal.adapter.common.JsonResult;
import com.life.goal.application.dto.request.LoginRequest;
import com.life.goal.application.dto.response.LoginResponse;
import com.life.goal.application.service.LifeTargetService;
import com.life.goal.application.service.UserService;
import com.life.goal.domain.entity.User;
import com.life.goal.infrastructure.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户登录相关功能的控制层
 */
@RestController
@RequestMapping("/life")
@ResponseBody
@Slf4j
public class LoginController {

    @Resource
    private UserService userService;

    @Autowired
    private LifeTargetService lifeTargetService;

    @PostMapping("/login")
    public JsonResult<LoginResponse> login(@RequestBody LoginRequest request) {

        User user = userService.login(request);

        return JsonResult.buildSuccess(LoginResponse.builder()
                .token(JwtUtil.geneJsonWebToken(user.getUserId(), user.getUserName()))
                .build());

    }

    @GetMapping("/insert")
    public String insert() {
        lifeTargetService.insert();
        return "insert success!";
    }
}
